package com.huarui.companypackage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.companypackage.model.CompanyService;
import com.huarui.companypackage.model.Service;
import com.huarui.companypackage.service.CompanyServerService;
import com.huarui.companypackage.service.ServerService;

/**
 * 赠送服务控制器
 * 
 * @author xu
 * 
 */
public class PresentServiceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6855582280275065960L;

	private Map<String, Object> resultMap;

	private String jsonData;

	private CompanyServerService companyServerService;

	private ServerService serverService;

	private Integer companyId;

	/**
	 * 赠送服务情况列表
	 * 
	 * @return
	 */
	public String listSendService() {
		resultMap = new HashMap<String, Object>();

		List<Service> services = serverService
				.getSelectableService(Service.SERVICE_PRESSENT);

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("companyId", companyId);
		List<Integer> ids = new ArrayList<Integer>();
		for (Service service : services) {
			ids.add(service.getId());
		}

		map.put("ids", ids);
		List<CompanyService> list = companyServerService
				.getCompanyServiceByCondition(map);

		resultMap.put("root", services);
		resultMap.put("present", list);
		return JSON;
	}

	public String presentSave() {
		JSONObject jo = JSONObject.fromObject(jsonData);

		companyId = jo.getInt("companyId");

		List<Integer> presentServices = new ArrayList<Integer>();

		if (jo.get("serviceIds") == null) {

		} else if (jo.get("serviceIds") instanceof String) {
			presentServices.add(jo.getInt("serviceIds"));
		} else {

			JSONArray ja = JSONArray.fromObject(jo.get("serviceIds"));

			presentServices = JSONArray.toList(ja, Integer.class);

		}

		List<Service> services = serverService.getSelectableService(Service.SERVICE_PRESSENT);
	
		
		resultMap = companyServerService
				.presentSave(companyId, presentServices,services);

		return JSON;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public String getJsonData() {
		return jsonData;
	}

	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}

	public void setCompanyServerService(
			CompanyServerService companyServerService) {
		this.companyServerService = companyServerService;
	}

	public void setServerService(ServerService serverService) {
		this.serverService = serverService;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

}
